package main.java.com.epam;

public class BinarySearch {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(binarySearch(19, new int[]{4, 15, 16, 17, 19, 42, 46, 95}));
	}
	
	public static int binarySearch(int num, int[] array){
		return binarySearch(num, 0, array.length, array);
	}

	private static int binarySearch(int num, int startIndex, int endIndex, int[] array){
		
		int indexToCheck = (endIndex + startIndex)/2;
		
		if(num == array[indexToCheck]) return indexToCheck;
		
		if(startIndex == endIndex - 1) return -1;
		
		if(num < array[indexToCheck]) return binarySearch(num, startIndex, indexToCheck, array);
		else return binarySearch(num, indexToCheck, endIndex, array);
	}
	
}
